package com.jblv.frame.web.controller.impl;

import com.jblv.frame.common.controller.BaseController;
import com.jblv.frame.common.dto.PageResult;
import com.jblv.frame.common.message.Result;
import com.jblv.frame.common.utils.StringUtils;
import com.jblv.frame.user.api.dto.SysMenuDTO;
import com.jblv.frame.user.api.entity.SysMenu;
import com.jblv.frame.user.api.params.SysMenuListParams;
import com.jblv.frame.user.api.params.SysMenuParams;
import com.jblv.frame.user.api.vo.SysMenuListVO;
import com.jblv.frame.web.controller.ISysMenuController;
import com.jblv.frame.web.facade.ISysMenuFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title SysMenuController
 * @projectName jblv-frame
 * @packageName com.jblv.web
 * @description SysMenuController 菜单Controller实现类
 * @date Create at 2020/5/26 22:24
 * @since jdk1.8
 */
@Api(tags = "菜单管理", value = "/v1/api/menu", description = "关于菜单相关接口")
@RestController
@RequestMapping("/v1/api/menu")
public class SysMenuController extends BaseController implements ISysMenuController {

    @Autowired
    ISysMenuFacade sysMenuFacade;


    @ApiOperation(value = "获取ID菜单信息", response = SysMenu.class, httpMethod = "GET", notes = "传入菜单信息ID")
    @Override
    @GetMapping("/id/{id}")
    public Result<SysMenu> get(@PathVariable("id") String id){
        return renderSuccess(sysMenuFacade.get(id));
    }

    @ApiOperation(value = "获取菜单列表", httpMethod = "POST", notes = "获取菜单列表说明")
    @Override
    @PostMapping("/list")
    public Result<PageResult<SysMenuDTO>> getList(@RequestBody SysMenuListParams params){
        if(StringUtils.isEmpty(params.getName())){
            return renderSuccess(sysMenuFacade.getList());
        }else{
            return renderSuccess(sysMenuFacade.getList(params));
        }

    }

    @ApiOperation(value = "创建菜单", httpMethod = "POST", notes = "传入菜单DTO")
    @Override
    @PostMapping("")
    public Result<String> save(@RequestBody SysMenuParams sysMenuParams) {
        return renderSuccess(sysMenuFacade.create(sysMenuParams));
    }

    @ApiOperation(value = "删除菜单", httpMethod = "DELETE", notes = "传入菜单ID")
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> del(@PathVariable String id) {
        return renderSuccess(sysMenuFacade.del(id));
    }

    @ApiOperation(value = "更新菜单", httpMethod = "PUT", notes = "传入菜单DTO")
    @Override
    @PutMapping("")
    public Result<Boolean> update(@RequestBody SysMenuParams sysMenuParams) {
        return renderSuccess(sysMenuFacade.update(sysMenuParams));
    }

    @Override
    @GetMapping("/getTree")
    public Result<List<SysMenuListVO>> getTree() {
        //List<FacadeSysMenuDTO> tree = GsonUtils.jsonToBean(returnTree, new TypeToken<List<FacadeSysMenuDTO>>() { }.getType());
        List<SysMenuListVO> temp = sysMenuFacade.getTreeList();
        return renderSuccess(temp);
    }
}
